package com.ekuaitu.kuaitu.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.transition.Explode;
import android.transition.Fade;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.BounceInterpolator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.utils.AppManager;
import com.ekuaitu.kuaitu.utils.DensityUtils;
import com.ekuaitu.kuaitu.utils.IntentKey;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HelpActivity extends Activity {


    @BindView(R.id.ad_content)
    TextView adContent;
    private ConvenientBanner convenientBanner;
    private List<String> titleList;
    private List<String> contentList;
    private Context mContext = this;
    private LinearLayout llPoints;
    private Activity activity = this;
    private TextView ad_title;

    private int tripMode;
    private List<Integer> localIv = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getAttributes().windowAnimations = R.style.DialogActivityShowAnimation;
        setContentView(R.layout.activity_help);
        ButterKnife.bind(this);
        this.setFinishOnTouchOutside(false);
        AppManager.getAppManager().addActivity(activity);
        initView();
        initData();
    }

    private void initData() {
        Intent intent = getIntent();
        tripMode = intent.getIntExtra(IntentKey.INTENT_TRIP_MODE, 2);
        if (tripMode == 2 || tripMode == -1) {
            localIv = new ArrayList<>();
            localIv.add(R.drawable.help_open);
            localIv.add(R.drawable.help_close);
            localIv.add(R.drawable.help_return);
            localIv.add(R.drawable.help_staff);
            titleList = new ArrayList<>();
            titleList.add("如何开锁");
            titleList.add("如何锁车");
            titleList.add("如何还车");
            titleList.add("客户服务");
            contentList = new ArrayList<>();
            contentList.add("在快兔单车的车身、车锁都有二维码，用手机扫码车锁会自动打开，订单开始计费。");
            contentList.add("用车后需手动合上锁环，听到“滴”一声则锁车成功。");
            contentList.add("锁上车锁后，点击还车按钮即可结束计费。");
            contentList.add("寻找客服帮助或发现故障车，请点击右下角客户服务图标。");
            initDots();
        } else if (tripMode == 1) {
            //汽车
            localIv = new ArrayList<>();
            localIv.add(R.drawable.help_order);
            localIv.add(R.drawable.help_open_car);
            localIv.add(R.drawable.help_return_car);
            titleList = new ArrayList<>();
            titleList.add("如何预约");
            titleList.add("如何开门");
            titleList.add("如何还车");
            contentList = new ArrayList<>();
            contentList.add("在地图上点击您要使用的车辆即可预约，预约车辆为您保留15分钟。");
            contentList.add("点击开门按钮则汽车车锁打开，订单开始计费。");
            contentList.add("汽车使用完成后，需将车辆停在网点内还车，然后点击还不按钮即可结束计费。");
            initDots();
        }


        convenientBanner.setPages(new CBViewHolderCreator<LocalImageHolderView>() {
            @Override
            public LocalImageHolderView createHolder() {
                return new LocalImageHolderView();
            }
        }, localIv)
                .setPointViewVisible(false)    //设置指示器是否可见
                .stopTurning();//关闭自动切换
        if (tripMode == 2) {
            ad_title.setText(titleList.get(0));
            adContent.setText(contentList.get(0));
        } else if (tripMode == 1) {
            ad_title.setText(titleList.get(0));
            adContent.setText(contentList.get(0));
        }


        convenientBanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < llPoints.getChildCount(); i++) {
                    llPoints.getChildAt(i).setBackgroundResource(R.drawable.dot_unchoose);
                    if (i == position % localIv.size()) {
                        llPoints.getChildAt(i).setBackgroundResource(R.drawable.dot_choose);
                    }
                }
                ad_title.setText(titleList.get(position));
                adContent.setText(contentList.get(position));
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void initView() {

        convenientBanner = (ConvenientBanner) findViewById(R.id.convenientBanner);
        llPoints = (LinearLayout) findViewById(R.id.llPoints);
        ad_title = (TextView) findViewById(R.id.ad_title);
    }

    private void initDots() {
        for (int i = 0; i < localIv.size(); i++) {
            View points = new View(mContext);
            points.setBackgroundResource(R.drawable.dot_unchoose);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(DensityUtils.dp2px(mContext, 6), DensityUtils.dp2px(mContext, 6));
            params.leftMargin = DensityUtils.dp2px(mContext, 10);
            points.setLayoutParams(params);
            llPoints.addView(points);
        }
        llPoints.getChildAt(0).setBackgroundResource(R.drawable.dot_choose);
    }

    @OnClick({R.id.banner_cancel, R.id.help_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.banner_cancel:
                finish();
                break;
            case R.id.help_login:
                startActivity(new Intent(HelpActivity.this, LoginActivity.class));
                break;
        }
    }

    public class LocalImageHolderView implements Holder<Integer> {
        private ImageView imageView;

        @Override
        public View createView(Context context) {
            imageView = new ImageView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, final int position, Integer data) {
            imageView.setImageResource(data);
        }
    }

    @Override
    public void onBackPressed() {
        finish();
    }
}
